package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //1、先查询购物车表中数据是否存在
        //找到指定用户的指定购物车数据
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //用户数据
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //指定用户ID，指定菜品id或者套餐id的购物车数据
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);

        if (shoppingCartList != null && shoppingCartList.size() > 0) {
            //2、存在，数量+1
            shoppingCart = shoppingCartList.get(0);
            int number = shoppingCart.getNumber() + 1;
            shoppingCart.setNumber(number);
            //更新数据到购物车表
            shoppingCartMapper.updateNumber(shoppingCart);


        } else {
            //3、将数据补充完整，插入到表中
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                //菜，查找该id对应具体菜的信息
                Dish dish = dishMapper.getById(dishId);
                //补充冗余字段信息: 图片，菜名，价格
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());

            } else {
                //套餐
                Long setmealId = shoppingCartDTO.getSetmealId();
                //获取指定id套餐的所有信息
                Setmeal setmeal = setmealMapper.getById(setmealId);
                //补充冗余字段信息: 图片，套餐名，价格
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
            }

            //补充字段
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            //插入到表中
            shoppingCartMapper.insert(shoppingCart);
        }


    }

    @Override
    public List<ShoppingCart> showShoppingCart() {
        //构建ShoppingCart对象，设置userid当做查询的条件
        ShoppingCart shoppingCar = ShoppingCart.builder().userId(BaseContext.getCurrentId()).build();
        return shoppingCartMapper.list(shoppingCar);
    }

    @Override
    public void cleanShoppingCart() {
        //根据当前用户id进行删除购物车数据
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(userId);
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart cart = ShoppingCart.builder().id(BaseContext.getCurrentId()).build();
        BeanUtils.copyProperties(shoppingCartDTO,cart);
        List<ShoppingCart> list = shoppingCartMapper.list(cart);
        if (list.size() > 0) {
            ShoppingCart shoppingCart = list.get(0);
            Integer newNum = shoppingCart.getNumber()-1;
            if (newNum <= 0) {
                //没有
                shoppingCartMapper.deleteById(shoppingCart.getId());
            } else {
                shoppingCart.setNumber(newNum);
                shoppingCartMapper.updateNumber(shoppingCart);
            }

        }
    }
}
